//
//  ViewController2.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/4/13.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources

class RxViewController2: BaseViewController {

    let disposeBag = DisposeBag()
    var tableView: UITableView!
    let reuserId = "reuserId"

    override func viewDidLoad() {
        super.viewDidLoad()
//        testUITableView()
        testRxTableView()
    }
}

extension RxViewController2 {
    func testUITableView() {

        tableView = UITableView(frame: self.view.bounds, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(MyTableViewCell.self, forCellReuseIdentifier: MyTableViewCell.description())
        self.view.addSubview(tableView)
    }
}

extension RxViewController2: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MyTableViewCell.description(), for: indexPath) as? MyTableViewCell
        cell?.getvalue(titleStr: "\(indexPath.row)", nameStr: "  Cooci  \(indexPath.row)")
        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(#function)
    }
}

extension RxViewController2 {

    fileprivate func testRxTableView() {

        tableView = UITableView(frame: self.view.bounds, style: .plain)
        tableView.register(MyTableViewCell.self, forCellReuseIdentifier: MyTableViewCell.description())
        self.view.addSubview(tableView)

        let items = Observable.just(NameSpace1.InfoViewModel().arr)
        items.bind(to: self.tableView.rx.items) { (tb, row, model) -> UITableViewCell in

            let cell = tb.dequeueReusableCell(withIdentifier: MyTableViewCell.description()) as? MyTableViewCell
            cell?.getvalue(titleStr: model.nameStr, nameStr: model.despStr)
            return cell!
        }.disposed(by: disposeBag)

        tableView.rx.modelSelected(NameSpace1.DataModel.self).subscribe(onNext: { (model) in
            print(model)
        }).disposed(by: disposeBag)


        tableView.rx.itemSelected.subscribe(onNext: { [weak self] indexpath in

            guard indexpath.row > 0 else {
                //行操作
                self?.tableView.isEditing = !(self?.tableView.isEditing ?? false)
                return
            }

            print("点击了 \(indexpath)")
            self?.navigationController?.pushViewController(TestViewController(), animated: true)
        }).disposed(by: disposeBag)


        tableView.rx.itemDeleted.subscribe(onNext: { (indexPath) in
            print("删除+++\(indexPath)")
        }).disposed(by: disposeBag)


        tableView.rx.itemMoved.subscribe(onNext: { (soureceIndex, desIndex) in
            print("从\(soureceIndex)移动到 \(desIndex)")
        }).disposed(by: disposeBag)
    }
}

